package Reminder.dao.impl;

import Reminder.dao.ScheduleDao;
import Reminder.domain.Message;
import Reminder.domain.Schedule;
import Reminder.domain.Utils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.IOException;
import java.text.ParseException;
import java.time.LocalTime;

public class ScheduleDaoImpl implements ScheduleDao {
    @Override
    public Message setScheduleData(int userId, JSONObject schedule) {
        Message msg = Utils.setTXT("data/schedule/"+ userId + ".txt",schedule.toString());
        if(msg.toJSON().getString("status").equals("success")){
            return new Message("setRemindData","存储成功","success");
        }
        else{
            return new Message("setRemindData","失败成功","success");
        }
    }

    @Override
    public Message getScheduleData(int userId) throws IOException, ParseException {
        Message msg = Utils.loadTXT("data/schedule/" + userId + ".txt");
        if(msg.toJSON().getString("status").equals("success")){
            return new Message("getRemindData",msg.toJSON().getString("data"),"success");
        }
        else{
            JSONObject jsonObject = new JSONObject();
            Schedule schedule = new Schedule(40,10,LocalTime.of(8,30),LocalTime.of(14,0),LocalTime.of(19,0),4,4,3);
            jsonObject.put("periodList",new JSONArray().toString());
            jsonObject.put("courseList",new JSONArray().toString());
            jsonObject.put("setting",schedule.exportSetting().toString());
            Utils.setTXT("data/schedule/" + userId + ".txt",jsonObject.toString());
            msg = Utils.loadTXT("data/schedule/" + userId + ".txt");
            if(msg.toJSON().getString("status").equals("success")){
                return new Message("getRemindData",msg.toJSON().getString("data"),"success");
            }
            else{
                return new Message("getRemindData","获取提醒信息成功失败","error");
            }

        }
    }
}
